package com.weijing.pretty_app.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weijing.pretty_app.R
import com.weijing.pretty_app.adapter.PartsAdapter
import com.weijing.pretty_app.adapter.TypeAdapter
import com.weijing.pretty_app.databinding.ActivityTuoMaoNextBinding
import com.weijing.pretty_app.models.SelectModel
import com.weijing.pretty_app.util.BluetoothSocketManager
import com.weijing.pretty_app.util.HexConverter
import com.weijing.pretty_app.util.KeyUtil

private const val TAG = "TuoMaoNextActivity"

/**
 * 脱毛选择肤色页面
 */
class TuoMaoNextActivity : BaseActivity() {

    private val binding by lazy { ActivityTuoMaoNextBinding.inflate(layoutInflater) }
    override fun getMainView(): View = binding.main

    private val partsList: MutableList<String> = mutableListOf()

    private val fuSeTextList: MutableList<Map<String, Int>> = mutableListOf()
    private var fangXiang = "L"
    private var fangXiangL = "L"
    private var fangXiangR = "R"
    private var sexL = 0 //默认女性,男士1 女士0
    private var sexR = 0 //默认女性,男士1 女士0
    private var locationL = 0 //默认脸部 0开始，0-5
    private var locationR = 0 //默认脸部 0开始，0-5
    private var colorL = 0 //肤色，默认Type I ,0-5
    private var colorR = 0 //肤色，默认Type I ,0-5
    private var buWeiCommand = "01"
    private var fuSeCommand = "01"
    private var mSelectMode: SelectModel? = null

    private var sexLCommand = "02" //性别：02-女 01-男  (默认女性)
    private var sexRCommand = "02" //性别：02-女 01-男  (默认女性)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        initClick()
        initPartsAdapter()
        initTypeAdapter()
    }

    private fun initClick() {
        binding.tuoMaoModeIvBack.setOnClickListener { finish() }

        binding.tuoMaoModeIvNext.setOnClickListener {
            val intent = Intent(this@TuoMaoNextActivity, HomeActivity::class.java)

            val maps = mutableMapOf<Int, SelectModel>()
            maps[0] = SelectModel(
                model1 = mSelectMode?.model1,
                model2 = mSelectMode?.model2,
                direction = fangXiangL,
                sex = sexL,
                location = locationL,
                color = colorL
            )
            maps[1] = SelectModel(
                model1 = mSelectMode?.model1, model2 = mSelectMode?.model2,
                direction = fangXiangR, sex = sexR, location = locationR, color = colorR
            )
            LiveEventBus.get<Map<Int, SelectModel>>(KeyUtil.TO_HOME_ACTIVITY).post(maps)
            sendData()
            startActivity(intent)
//            finish()
        }

        //选择女性
        binding.tuoMaoNextLlNV.setOnClickListener {
            when (fangXiang) {
                "L" -> {
                    sexL = 0
                    sexLCommand = "02"
                }

                "R" -> {
                    sexR = 0
                    sexRCommand = "02"
                }
            }

            binding.tuoMaoNextTvNV.setTextColor(ContextCompat.getColor(this, R.color.white))
            binding.tuoMaoNextTvNAN.setTextColor(ContextCompat.getColor(this, R.color.color_00C1CD))
            binding.tuoMaoNextIvTriangleNv.visibility = View.VISIBLE
            binding.tuoMaoNextIvTriangleNan.visibility = View.INVISIBLE
        }

        //选择男性
        binding.tuoMaoNextLlNAN.setOnClickListener {
            when (fangXiang) {
                "L" -> {
                    sexL = 1
                    sexLCommand = "01"
                }

                "R" -> {
                    sexR = 1
                    sexRCommand = "01"
                }
            }

            binding.tuoMaoNextTvNAN.setTextColor(ContextCompat.getColor(this, R.color.white))
            binding.tuoMaoNextTvNV.setTextColor(ContextCompat.getColor(this, R.color.color_00C1CD))
            binding.tuoMaoNextIvTriangleNv.visibility = View.INVISIBLE
            binding.tuoMaoNextIvTriangleNan.visibility = View.VISIBLE
        }

        binding.tuoMaoNextClL.setOnClickListener {
            fangXiang = "L"
            setActiveButton(true)

            binding.tuoMaoNextIvOrderL.visibility = View.VISIBLE
            binding.tuoMaoNextIvOrderR.visibility = View.INVISIBLE
        }
        binding.tuoMaoNextClR.setOnClickListener {
            fangXiang = "R"

            setActiveButton(false)
            binding.tuoMaoNextIvOrderL.visibility = View.INVISIBLE
            binding.tuoMaoNextIvOrderR.visibility = View.VISIBLE
        }

        LiveEventBus.get<SelectModel>(KeyUtil.TO_NEXT_ACTIVITY).observeSticky(this) {
            mSelectMode = it
        }
    }

    private fun setActiveButton(isLeftActive: Boolean) {
        if (isLeftActive) {
            binding.tuoMaoNextClL.background =
                ContextCompat.getDrawable(this, R.drawable.shape_white_top30)
            binding.tuoMaoNextClR.background =
                ContextCompat.getDrawable(this, R.drawable.shape_00c1cd_bottom30)

            binding.tuoMaoNextIvL.setImageResource(R.mipmap.sb_00c1cd)
            binding.tuoMaoNextIvR.setImageResource(R.mipmap.sb_white)

            binding.tuoMaoNextTvL.setBackgroundResource(R.drawable.shape_circle_00c1cd)
            binding.tuoMaoNextTvR.setBackgroundResource(R.drawable.shape_circle_whit)

            binding.tuoMaoNextTvL.setTextColor(ContextCompat.getColor(this, R.color.color_00C1CD))
            binding.tuoMaoNextTvR.setTextColor(ContextCompat.getColor(this, R.color.white))
        } else {
            binding.tuoMaoNextClR.background =
                ContextCompat.getDrawable(this, R.drawable.shape_white_bottom30)
            binding.tuoMaoNextClL.background =
                ContextCompat.getDrawable(this, R.drawable.shape_00c1cd_top30)

            binding.tuoMaoNextIvR.setImageResource(R.mipmap.sb_00c1cd)
            binding.tuoMaoNextIvL.setImageResource(R.mipmap.sb_white)

            binding.tuoMaoNextTvR.setBackgroundResource(R.drawable.shape_circle_00c1cd)
            binding.tuoMaoNextTvL.setBackgroundResource(R.drawable.shape_circle_whit)
            binding.tuoMaoNextTvR.setTextColor(ContextCompat.getColor(this, R.color.color_00C1CD))
            binding.tuoMaoNextTvL.setTextColor(ContextCompat.getColor(this, R.color.white))
        }

        binding.tuoMaoNextClL.requestLayout()
        binding.tuoMaoNextClR.requestLayout()
    }

    //身体部位适配器
    private fun initPartsAdapter() {
        partsList.add(resources.getString(R.string.face))
        partsList.add(resources.getString(R.string.Underarms))
        partsList.add(resources.getString(R.string.Arms))
        partsList.add(resources.getString(R.string.Abdomen))
        partsList.add(resources.getString(R.string.Bikini))
        partsList.add(resources.getString(R.string.Legs))
        val manager = GridLayoutManager(this, 3)
        binding.tuoMaoNextRV1.layoutManager = manager
        val adapter = PartsAdapter(partsList)
        binding.tuoMaoNextRV1.adapter = adapter
        adapter.onClickListener = object : PartsAdapter.OnClickListener {
            override fun onItemClickListener(position: Int, name: String) {
                when (fangXiang) {
                    "L" -> {
                        locationL = position
                    }

                    "R" -> {
                        locationR = position
                    }
                }
                when (position) {
                    0 -> {
                        buWeiCommand = "01"
                    }

                    1 -> {
                        buWeiCommand = "02"
                    }

                    2 -> {
                        buWeiCommand = "03"
                    }

                    3 -> {
                        buWeiCommand = "04"
                    }

                    4 -> {
                        buWeiCommand = "05"
                    }

                    5 -> {
                        buWeiCommand = "06"
                    }
                }

                adapter.setIndex(position)
            }
        }
    }

    private fun initTypeAdapter() {
        fuSeTextList.add(mapOf(getString(R.string.Type_I) to R.mipmap.icon_type1))
        fuSeTextList.add(mapOf(getString(R.string.Type_II) to R.mipmap.icon_type2))
        fuSeTextList.add(mapOf(getString(R.string.Type_III) to R.mipmap.icon_type3))
        fuSeTextList.add(mapOf(getString(R.string.Type_IV) to R.mipmap.icon_type4))
        fuSeTextList.add(mapOf(getString(R.string.Type_V) to R.mipmap.icon_type5))
        fuSeTextList.add(mapOf(getString(R.string.Type_VI) to R.mipmap.icon_type6))

        val adapter = TypeAdapter(fuSeTextList)
        binding.tuoMaoNextRV2.adapter = adapter
        adapter.onClickListener = object : TypeAdapter.OnClickListener {
            override fun onItemClickListener(position: Int, type: String) {
                when (fangXiang) {
                    "L" -> {
                        colorL = position
                    }

                    "R" -> {
                        colorR = position
                    }
                }
                when (position) {
                    0 -> {
                        fuSeCommand = "01"
                    }

                    1 -> {
                        fuSeCommand = "02"
                    }

                    2 -> {
                        fuSeCommand = "03"
                    }

                    3 -> {
                        fuSeCommand = "04"
                    }

                    4 -> {
                        fuSeCommand = "05"
                    }

                    5 -> {
                        fuSeCommand = "06"
                    }
                }

                adapter.setIndex(position)
            }
        }
    }

    private fun sendData() {
        val commentAll =
            KeyUtil.TUO_MAO_ZHI_NENG +
                    sexLCommand +
                    " 0" + (colorL + 1) +
                    " 0" + (locationL + 1) +
                    " " +
                    sexRCommand +
                    " 0" + (colorR + 1) +
                    " 0" + (locationR + 1) +
                    " " +
                    KeyUtil.EE
        Log.i(TAG, "脱毛智能-- ${commentAll}")
        val data = HexConverter.hexStringToByteArray(commentAll)
        BluetoothSocketManager.send(data)
    }
}